<script setup>
import Info from './info.vue'
import {onMounted, ref} from 'vue'
import {pageApi,deleteApi} from "../../../api/student";
import {ElMessage, ElMessageBox} from "element-plus";
import {useUserStore} from "../../../stores/userstore";

const store=useUserStore()
const tableData = ref({})
const token=ref({
  satoken:store.token
})
// 表单状态
const filterState = ref(true)
const dates=ref([])
// 表单数据 -- 查询条件
const formData = ref({
  page: 1,
  size: 10,
  name: '',
  deptName:''
})

onMounted(async ()=>{
  loadData()
})

const loadData=async ()=>{
  const r=await pageApi(formData.value)
  tableData.value=r.data;
}
// 编辑
const authEditRef = ref()
const onAuthEdit = (row) => {
  authEditRef.value.state = true
  // 回显
  if (row) {
    authEditRef.value.formData.id = row.id
    authEditRef.value.formData.sex = row.sex
    authEditRef.value.formData.no = row.no 
    authEditRef.value.formData.gid = row.gid
    authEditRef.value.formData.deptName = row.deptName
  }
}

// 学员删除
const onDelete = (row) => {
  ElMessageBox.confirm('确定删除该条数据吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    //接口请求 删除学员
    const r=await deleteApi({id:row.id});
    if(r.code==0){
      ElMessage.success("删除成功！")
        //重新加载请求
        loadData()
      }else {
        ElMessage.error(r.data)
      }
  })
}
const showExcel=ref(false)

const handleSuccess=(res)=>{
  if(res.code==0){
    //上传成功
    ElMessage.success(res.data)
    loadData()
    showExcel.value=false
    console.log(res.data)
  }else{
    ElMessage.error(res.data)
  }
}
</script>
<template>
  <Box>
    <!-- 逻辑层 -->
    <Info ref="authEditRef" @flush="loadData"/>
    <!-- 逻辑层 -->

    <!-- 筛选 -->
    <template #filter v-if="filterState">
      <el-form :inline="true" :model="formData">
        <el-form-item label="学员姓名">
          <el-input placeholder="请输入学员姓名" v-model="formData.name" />
        </el-form-item>
        <el-form-item label="部门名称">
          <el-input placeholder="请输入部门名称" v-model="formData.deptName" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="loadData"> <span class="iconfont icon-sousuo"></span>搜索 </el-button>
          <el-button type="default"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </template>
    <!-- 筛选 -->

    <!-- 按钮组 -->
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="onAuthEdit">
          <span class="iconfont icon-jiahao"></span>添加
        </el-button>
         <el-button type="primary" @click="showExcel=true">
          <span class="iconfont icon-jiahao"></span>Excel上传
        </el-button>
      </el-col>

      <el-col :span="12" style="text-align: right">
        <el-button circle @click="filterState = !filterState">
          <span class="iconfont icon-sousuo"></span>
        </el-button>
        <el-button circle>
          <span class="iconfont icon-shuaxin"></span>
        </el-button>
      </el-col>
    </el-row>
    <!-- 按钮组 -->

    <el-table :data="tableData.data" :border="true" class="qfTable" row-key="id" default-expand-all>
      <el-table-column label="序号" prop="id" align="center"></el-table-column>
      <el-table-column label="姓名" prop="name" align="center"></el-table-column>
      <el-table-column label="部门名称" prop="deptName" align="center"></el-table-column>
      <el-table-column label="性别" prop="sex" align="center"></el-table-column>
      <el-table-column label="工号" prop="no" align="center"></el-table-column>
      <el-table-column label="创建时间" prop="ctime" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template #default="scoped">
          <el-button type="primary" link size="small" @click="onAuthEdit(scoped.row)">
            <span class="iconfont icon-bianji"></span>修改
          </el-button>
          <el-button type="primary" link size="small" @click="onDelete(scoped.row)">
            <span class="iconfont icon-shanchu"></span>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
        v-model:current-page="formData.page"
        v-model:page-size="formData.size"
        :page-sizes="[10, 20, 30, 40]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
        @size-change="loadData"
        @current-change="loadData"
    />

     <el-dialog
    v-model="showExcel"
    title="上传学员Excel文件"
    width="600"
    :before-close="() => (showExcel = false)"
  >
     <el-form-item label="Excel文件">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:8080/api/student/importexcel"
            :headers="token"
            :show-file-list="false"
            :on-success="handleSuccess"
        > 选择上传的Excel文件
        </el-upload>     
      </el-form-item>
     </el-dialog>
  </Box>
</template>

<style lang="scss" scoped></style>
